# -*- coding: utf-8 -*-
# @Time:  2025/7/20 17:55
# @File:  insert.py
# @Action:
# scripts/seed_data.py
import random
from sqlmodel import Session, create_engine
from faker import Faker  # 需要安装: pip install faker

from models import User
from database import SQLITE_URL

# 创建Faker实例生成模拟数据
fake = Faker('zh_CN')

# 连接数据库
engine = create_engine(SQLITE_URL, connect_args={"check_same_thread": False})


def seed_users(count=20):
    """插入测试用户数据"""
    with Session(engine) as session:
        # 先清空现有数据
        # session.query(User).delete()

        # 生成测试数据
        for _ in range(count):
            user = User(
                username=fake.name(),
                email=fake.email(),
                gender=random.randint(1, 2),
                age=random.randint(18, 60)
            )
            session.add(user)

        session.commit()
        print(f"成功插入 {count} 条用户数据")


if __name__ == "__main__":
    seed_users(20)